package com.itheima.controller;

import com.itheima.entity.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @PackageName: com.itheima.controller
 * @ClassName: UserController
 * @Author: zhangwendong
 * @Date: 2020/4/25 21:45
 * @Description: 用户功能
 */
@RequestMapping("/user")
@RestController
public class UserController {

    @RequestMapping("/findUser")
    public Result findUser() {
        //通过安全框架获取用户名
        try {
            String name = SecurityContextHolder.getContext().getAuthentication().getName();
            return new Result(name);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }

    }
    /*
    * @Description :用户登录失败错误信息功能
    * @Date 20:26 2020/4/27
    **/
    @RequestMapping("/getErrorMessage")
    public Map getErrorMessage(HttpSession session){
        Map map = new HashMap();
        Exception e = (Exception)session.getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
        map.put("error",e.getMessage());
        return map;
    }
}
